package com.spark.laboratory.controller;

import com.spark.laboratory.pojo.Users;
import com.spark.laboratory.pojo.vo.UserVO;
import com.spark.laboratory.service.UsersService;
import com.spark.laboratory.utils.FengliJsonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;


/**
 * 用户注册登录接口
 *
 * @author lixizheng
 * @date 2020/07/02
 */
@RestController()
public class RegisterLoginController {

    private final UsersService usersService;

    @Autowired
    public RegisterLoginController(UsersService usersService) {
        this.usersService = usersService;
    }

    /**
     * 注册
     *
     * @param user 用户对象
     * @return json数据
     */
    @PostMapping("/regist")
    public FengliJsonResult regist(Users user) {

        System.out.println(user);
        // 判断是在校还是非在校生
        if ("".equals(user.getUserStudentNumber()) && "已毕业".equals(user.getUserClass())) {
            // 非在校生
            if (StringUtils.isBlank(user.getUserName()) || StringUtils.isBlank(user.getUserSchool())) {
                return FengliJsonResult.errorMsg("非在校生~ 姓名和学校名称不能为空");
            }

            // 判断用户是否存在
            Users users = usersService.queryUserIsExist_2(user.getUserSchool(), user.getUserName());

            if (users != null) {
                return FengliJsonResult.errorMsg("非在校生~ 姓名和学校名称已经存在，请换一个再试");
            } else {
                user.setCreateDate(new Date());
                user.setIsDelete(0);

                usersService.saveUser(user);
            }

            UserVO userVO = new UserVO();

            return FengliJsonResult.ok(userVO);
        } else {
            // 在校生
            if (StringUtils.isBlank(user.getUserStudentNumber()) || StringUtils.isBlank(user.getUserSchool()) || StringUtils.isBlank(user.getUserName())) {
                return FengliJsonResult.errorMsg("在校生~ 学号和学校名称、姓名不能为空");
            }

            // 判断用户是否存在
            Users users = usersService.queryUserIsExist_1(user.getUserSchool(), user.getUserStudentNumber(),user.getUserName());

            if (users != null) {
                return FengliJsonResult.errorMsg("在校生~ 学号和学校名称已经存在，请换一个再试");
            } else {
                user.setCreateDate(new Date());
                user.setIsDelete(0);
                usersService.saveUser(user);
            }

            UserVO userVO = new UserVO();
            return FengliJsonResult.ok(userVO);
        }
    }

    /**
     * 登录
     *
     * @param user     用户对象
     * @param session  服务器端会话
     * @return json数据
     */
    @PostMapping("/login")
    public FengliJsonResult login(Users user, HttpSession session) {

        // 返回前端对象
        UserVO userVO = new UserVO();

        if ("".equals(user.getUserStudentNumber())){
            // 没有学号 判断为非在校生
            if (StringUtils.isBlank(user.getUserName()) || StringUtils.isBlank(user.getUserSchool())) {
                return FengliJsonResult.errorMsg("登录失败~ 姓名和学校名称不能为空");
            }

            // 判断用户是否存在
            Users users = usersService.queryUserIsExist_2(user.getUserSchool(), user.getUserName());

            if (users != null) {
                session.setAttribute("userId", users.getId());
                session.setAttribute("userName", users.getUserName());

            } else {
                userVO.setErrorInfo("姓名和学校名称不正确, 请重试...");
            }

        }else {
            // 在校生
            if (StringUtils.isBlank(user.getUserStudentNumber()) || StringUtils.isBlank(user.getUserSchool()) || StringUtils.isBlank(user.getUserName())) {
                return FengliJsonResult.errorMsg("登录失败~ 学号、学校名称、姓名不能为空");
            }

            // 判断用户是否存在
            Users users = usersService.queryUserIsExist_1(user.getUserSchool(), user.getUserStudentNumber(), user.getUserName());

            System.out.println(users);

            if (users != null) {
                System.out.println("-------------------------");
                session.setAttribute("userId", users.getId());
                session.setAttribute("userName", users.getUserName());

            } else {
                userVO.setErrorInfo("学号、学校名称、姓名不正确, 请重试...");
            }
        }
        return FengliJsonResult.ok(userVO);
    }

    /**
     * 注销
     * @param request http 请求对象
     * @return json数据
     */
    @PostMapping("/logout")
    public FengliJsonResult logout(HttpServletRequest request) {
        HttpSession session = request.getSession();

        try {
            session.removeAttribute("userId");
            session.removeAttribute("userName");


        }catch (Exception e){
            e.printStackTrace();
        }

        return FengliJsonResult.ok();
    }

    /**
     * 专家入口
     * @param session session对象
     * @return session 设置状态
     */
    @RequestMapping("/expertsEntrance")
    public FengliJsonResult expertsEntrance(HttpSession session){
        UUID uuid = UUID.randomUUID();
        session.setAttribute("userName", "专家" + uuid);

        return FengliJsonResult.ok("ok");
    }
}
